﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHose.Application.Admins.Dto;
using WareHose.Application.Roles.DTO;
using WareHouse.Domain;
using WareHouse.Repositories;

namespace WareHose.Application.Roles
{
    //角色
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v2")]


    public class RoleService : IDynamicWebApi   
    {

        //仓储
        private readonly IBaseRepository<Role> db;
        //AutoMapper
        private readonly IMapper mapper;

        public RoleService(IBaseRepository<Role> db, IMapper mapper)
        {
            this.mapper = mapper;
            this.db = db;
        }


        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPost]
        [AllowAnonymous]
        public async Task<ResultDto> AddUpdAsync(RoleInputDTo RoleInputDTo)
        {
            try
            {

                var entity = mapper.Map<RoleInputDTo, Role>(RoleInputDTo);
                entity.AddDate=DateTime.Now;
                await db.InsertAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "添加成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }



        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="RoleRequestDTo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultDtos<List<Role> >> AdminPage(string? RoleName,string? Remark, int pageIndex = 1, int pageSize = 4)
        {

            var list = from a in await db.QueryAsync()
                       select new Role
                       {
                           RoleName = a.RoleName,
                           RoleId = a.RoleId,
                           AddPerson = a.AddPerson,
                           AddDate = a.AddDate,
                           Remark = a.Remark,
                           RoleCode = a.RoleCode,
                       };
         
            if (!string.IsNullOrEmpty(RoleName))
            {
                list = list.Where(m => m.RoleName.Contains(RoleName));
            }
            if (!string.IsNullOrEmpty(Remark))
            {
                list = list.Where(m => m.Remark.Contains(Remark));
            }
            int  listcount = list.Count();
            list = list.OrderBy(x => x.RoleId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  
            return new ResultDtos<List<Role> >
            {
                Code = Status.Success,
                Data = list.ToList(),
                Listcount = listcount
            };
        }



        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="RoldId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultDto> DeleRoleAsync(string RoldId)
        {
            if(RoldId!=null)
            {
                string[] ids = RoldId.Split(',');
                foreach (var item in ids)
                {
                    await db.DeleAsync(Convert.ToInt64(item));
                }

                return new ResultDto { Code = Status.Success, Message = "删除成功" };
            }
            else
            {
                return new ResultDto { Code = Status.Success, Message = "删除失败" };
            }
           
          
        }




        //public async Task<ResultDto> Deletostring(string RoldId)
        //{



        //    string[] ids = RoldId.Split(',');
        //    foreach (var item in ids)
        //    {
        //        var list = Convert.ToInt32(item);
        //        await db.DeleAsyncList(list);

        //        return new ResultDto { Code = Status.Success, Message = "删除成功" };
        //    }






        //}




        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPut]
       public async Task<ResultDto> RoleUpdateAsync(RoleInputDTo RoleInputDTo)
        {
            try
            {
               
                    var entity = mapper.Map<RoleInputDTo, Role>(RoleInputDTo);
                    await db.UpdateAsync(entity);
                    return new ResultDto { Code = Status.Success, Message = "修改成功" };
                
            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ResultDto<Role>>  BackFill(int id)
        {
            var list = await db.FistAsync(x => x.RoleId == id);

            return new ResultDto<Role> { Code = Status.Success, Message = "成功", Data = list };
        }


        }
    }
